package com.wgts.wgts.mvp.view.activity.todayspecial;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.behavior.awardmore.AlaphTitleBehavior;
import com.wgts.wgts.control.refresh.ClassicsFoot;
import com.wgts.wgts.control.refresh.ClassicsFoot;
import com.wgts.wgts.control.refresh.ClassicsFoot;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerHomePageCurrencyActivityComponent;
import com.wgts.wgts.dagger.module.HomePageCurrencyActivityModule;
import com.wgts.wgts.mvp.presenter.IHomePageCurrencyActivityPresenter;
import com.wgts.wgts.mvp.view.IHomePageCurrencyActivityView;
import com.wgts.wgts.mvp.view.activity.goodsinfo.JDGoodinfoActivity;
import com.wgts.wgts.netmodle.resp.ComponentIndextActivityEntity;
import com.wgts.wgts.netmodle.resp.SpecialProductEntity;
import com.wgts.wgts.tools.CustomImageView;
import com.wgts.wgts.tools.MoHuGlide;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 今日活动
 */
public class HomePageCurrencyActivity extends BaseActivity implements IHomePageCurrencyActivityView {


    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R.id.img_back_init)
    ImageView imgBackInit;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_suspension_title)
    TextView tvSuspensionTitle;
    @BindView(R.id.relative_suspension)
    LinearLayout relativeSuspension;
    @BindView(R.id.img_title)
    ImageView imgTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar_ly)
    CollapsingToolbarLayout toolbarLy;
    @BindView(R.id.coordinator_ly)
    CoordinatorLayout coordinatorLy;
    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;
    @BindView(R.id.net_error_ly)
    LinearLayout netErrorLy;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.foot_view)
    ClassicsFoot footView;

    private List<SpecialProductEntity.Entity> productEntities;

    public static final String ACTIVITYID = "activityId";
    public static final String POSITION = "position";

    public static final String TITLE = "title";
    private String position;


    private HomePageCurrencyAdapter adapter;


    private LinearLayoutManager linearLayoutManager;


    @Inject
    IHomePageCurrencyActivityPresenter presenter;

    @Override
    public void init() {
        productEntities = new ArrayList<>();
        DaggerHomePageCurrencyActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .homePageCurrencyActivityModule(new HomePageCurrencyActivityModule(this))
                .build()
                .inject(this);

    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.new_people_action_activity);
        ButterKnife.bind(this);
        position = getIntent().getStringExtra(POSITION);
        initView();
    }

    private void initView() {
        load();
        refreshLayout.setEnableRefresh(false);
        switch (position) {
            case "0":
                coordinatorLy.setBackgroundResource(R.color.new_people_bg);
                toolbarLy.setBackgroundResource(R.color.new_people_bg);
                footView.setBackgroundResource(R.color.new_people_bg);
                break;
            case "1":
                coordinatorLy.setBackgroundResource(R.color.new_good_bg);
                toolbarLy.setBackgroundResource(R.color.new_good_bg);
                footView.setBackgroundResource(R.color.new_good_bg);
                break;
            case "2":
                coordinatorLy.setBackgroundResource(R.color.today_sale_bg);
                toolbarLy.setBackgroundResource(R.color.today_sale_bg);
                footView.setBackgroundResource(R.color.today_sale_bg);
                break;
            case "3":
                coordinatorLy.setBackgroundResource(R.color.brand_commend_bg);
                toolbarLy.setBackgroundResource(R.color.brand_commend_bg);
                footView.setBackgroundResource(R.color.brand_commend_bg);
                break;
            case "4":
                coordinatorLy.setBackgroundResource(R.color.platform_commend_bg);
                toolbarLy.setBackgroundResource(R.color.platform_commend_bg);
                footView.setBackgroundResource(R.color.platform_commend_bg);
                break;
        }
        CustomImageView.getInstance().customImage(this, imgTitle, (422 / (double) 750));
        tvSuspensionTitle.setText(getIntent().getStringExtra(TITLE));
        linearLayoutManager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(linearLayoutManager);
        adapter = new HomePageCurrencyAdapter(this, productEntities);
        recycleView.setAdapter(adapter);

//        recycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//                int firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
//                if (firstVisibleItem == 0) {
//                    relativeSuspension.setVisibility(View.GONE);
//                } else {
//                    relativeSuspension.setVisibility(View.VISIBLE);
//                }
//            }
//        });

        adapter.setOnClickListener(new HomePageCurrencyAdapter.OnClickListener() {
            @Override
            public void onClick(View view, int i) {
                Intent intent = new Intent(HomePageCurrencyActivity.this, JDGoodinfoActivity.class);
                intent.putExtra(JDGoodinfoActivity.JOIN_TYPE, JDGoodinfoActivity.NOMAL);
                intent.putExtra("id", String.valueOf(productEntities.get(i).productId));
                startActivity(intent);
            }
        });

        //presenter.initHeader();
        showDialog();
        presenter.initProduct(Integer.valueOf(getIntent().getStringExtra(ACTIVITYID)));

        presenter.initHeader(Integer.valueOf(getIntent().getStringExtra(ACTIVITYID)));
    }

    @Override
    public void initProductResult(SpecialProductEntity entity, boolean noMore) {
        dialogDismiss();
        AlaphTitleBehavior downTitleBehavior = new AlaphTitleBehavior();
        CoordinatorLayout.LayoutParams params =
                (CoordinatorLayout.LayoutParams) relativeSuspension.getLayoutParams();
        params.setBehavior(downTitleBehavior);
        adapter.setNoMore(noMore);
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
        if (entity.list.size() < 1) {
            netErrorLy.setVisibility(View.GONE);
            noneDataLy.setVisibility(View.VISIBLE);
        } else {
            netErrorLy.setVisibility(View.GONE);
            noneDataLy.setVisibility(View.GONE);
        }
        productEntities.clear();
        productEntities.addAll(entity.list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void moreProductResult(SpecialProductEntity entity, boolean noMore) {
        refreshLayout.finishLoadmore();
        adapter.setNoMore(noMore);
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
        productEntities.addAll(entity.list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void initHeader(List<ComponentIndextActivityEntity> componentIndextActivityEntities) {
        if (componentIndextActivityEntities.size() > 0 && componentIndextActivityEntities.get(0).result.size() > 0) {
            MoHuGlide.getInstance().glideImg(this, componentIndextActivityEntities.get(0).result.get(0).sourceUrl, imgTitle);
            tvTitle.setText(componentIndextActivityEntities.get(0).result.get(0).desc);
        }
    }


    @Override
    public void onError(int status, String msg) {
        dialogDismiss();
        MoHuToast.showToast(msg);
        netErrorLy.setVisibility(View.VISIBLE);
        noneDataLy.setVisibility(View.GONE);
    }

    @OnClick({R.id.img_back_init, R.id.img_back, R.id.net_error_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back_init:
                finish();
                break;
            case R.id.img_back:
                finish();
                break;
            case R.id.net_error_ly:
                presenter.initHeader(Integer.valueOf(getIntent().getStringExtra(ACTIVITYID)));
                presenter.initProduct(Integer.valueOf(getIntent().getStringExtra(ACTIVITYID)));
                break;
        }
    }

    private void load() {
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.moreProduct();
            }
        });
    }
}
